package io.github.uicdb.gravity_generator.data.loot;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import io.github.uicdb.gravity_generator.GGRegistries;
import net.minecraft.core.Registry;
import net.minecraft.data.loot.BlockLoot;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.storage.loot.LootTable;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class GGBlockLoot extends BlockLoot {
    private final Map<ResourceLocation, LootTable.Builder> map = Maps.newHashMap();
    @Override
    protected void addTables() {
        dropSelf(GGRegistries.GRAVITY_GENERATOR.get());
        dropSelf(GGRegistries.HARMLESS_GRAVITY_GENERATOR.get());
    }
    @Override
    public void accept(@NotNull BiConsumer<ResourceLocation, LootTable.Builder> builderBiConsumer) {
        this.addTables();
        Set<ResourceLocation> set = Sets.newHashSet();
        for(Block block : Registry.BLOCK) {
            ResourceLocation resourcelocation = block.getLootTable();
            if (resourcelocation != BuiltInLootTables.EMPTY && set.add(resourcelocation)) {
                LootTable.Builder builder = this.map.remove(resourcelocation);
                if (builder != null) {
                    builderBiConsumer.accept(resourcelocation, builder);
                }
            }
        }
    }

    @Override
    protected void add(@NotNull Block block, @NotNull LootTable.Builder builder) {
        this.map.put(block.getLootTable(),builder);
    }
}
